package com.guofen.service.sjq.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.SjqDeodorizationRunningThreeMapper;
import com.guofen.pojo.SjqDeodorizationRunningThree;
import com.guofen.service.sjq.SjqDeodorizationRunningThreeService;
import com.guofen.util.DateAndTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SjqDeodorizationRunningThreeServiceImpl implements SjqDeodorizationRunningThreeService {

    @Autowired
    private SjqDeodorizationRunningThreeMapper sjqDeodorizationRunningThreeMapper;

    @Override
    public boolean addForm(String solidLiquidAtomizingSystem, String solidLiquidFilter1, String solidLiquidFilter2,
                                  String solidLiquidSprayNozzle1, String solidLiquidSprayNozzle2, String solidLiquidSprayFilter1,
                                  String solidLiquidSprayFilter2, String solidLiquidFanOperation1, String solidLiquidFanOperation2,
                                  String compostAtomizingSystem, String compostFilter1, String compostFilter2,
                                  String compostSprayNozzle1, String compostSprayNozzle2, String compostSprayFilter1,
                                  String compostSprayFilter2, String compostFanOperation1, String compostFanOperation2,
                                  String flocculationAtomizingSystem, String flocculationPlasmaSystem, String flocculationFanOperation,
                                  String operator, String examiner, String remarks) {
        //获取当前日期
        String date = DateAndTimeUtil.getDate();
        SjqDeodorizationRunningThree sjqDeodorizationRunningThree = new SjqDeodorizationRunningThree();
        sjqDeodorizationRunningThree.setDate(date);
        sjqDeodorizationRunningThree.setSolidLiquidAtomizingSystem(solidLiquidAtomizingSystem);
        sjqDeodorizationRunningThree.setSolidLiquidFilter1(solidLiquidFilter1);
        sjqDeodorizationRunningThree.setSolidLiquidFilter2(solidLiquidFilter2);
        sjqDeodorizationRunningThree.setSolidLiquidSprayNozzle1(solidLiquidSprayNozzle1);
        sjqDeodorizationRunningThree.setSolidLiquidSprayNozzle2(solidLiquidSprayNozzle2);
        sjqDeodorizationRunningThree.setSolidLiquidSprayFilter1(solidLiquidSprayFilter1);
        sjqDeodorizationRunningThree.setSolidLiquidSprayFilter2(solidLiquidSprayFilter2);
        sjqDeodorizationRunningThree.setSolidLiquidFanOperation1(solidLiquidFanOperation1);
        sjqDeodorizationRunningThree.setSolidLiquidFanOperation2(solidLiquidFanOperation2);
        sjqDeodorizationRunningThree.setCompostAtomizingSystem(compostAtomizingSystem);
        sjqDeodorizationRunningThree.setCompostFilter1(compostFilter1);
        sjqDeodorizationRunningThree.setCompostFilter2(compostFilter2);
        sjqDeodorizationRunningThree.setCompostSprayNozzle1(compostSprayNozzle1);
        sjqDeodorizationRunningThree.setCompostSprayNozzle2(compostSprayNozzle2);
        sjqDeodorizationRunningThree.setCompostSprayFilter1(compostSprayFilter1);
        sjqDeodorizationRunningThree.setCompostSprayFilter2(compostSprayFilter2);
        sjqDeodorizationRunningThree.setCompostFanOperation1(compostFanOperation1);
        sjqDeodorizationRunningThree.setCompostFanOperation2(compostFanOperation2);
        sjqDeodorizationRunningThree.setFlocculationAtomizingSystem(flocculationAtomizingSystem);
        sjqDeodorizationRunningThree.setFlocculationPlasmaSystem(flocculationPlasmaSystem);
        sjqDeodorizationRunningThree.setFlocculationFanOperation(flocculationFanOperation);
        sjqDeodorizationRunningThree.setOperator(operator);
        sjqDeodorizationRunningThree.setExaminer(examiner);
        sjqDeodorizationRunningThree.setRemarks(remarks);

        int result = sjqDeodorizationRunningThreeMapper.insert(sjqDeodorizationRunningThree);
        if (result == 1)
            return true;
        else
            return false;
    }

    @Override
    public SjqDeodorizationRunningThree findForm() {
        //获取当前日期
        String date = DateAndTimeUtil.getDate();
        System.out.println("当前日期" + date);
        //获取最新记录
        SjqDeodorizationRunningThree sjqDeodorizationRunningThree = sjqDeodorizationRunningThreeMapper.findLast();
        System.out.println("最新记录" + sjqDeodorizationRunningThreeMapper.findLast());
        //获取最新记录日期
        String dateLast =  sjqDeodorizationRunningThree.getDate();
        System.out.println("最新记录日期" + dateLast);

        if (date.equals(dateLast))
            return sjqDeodorizationRunningThree;
        else
            return null;
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<SjqDeodorizationRunningThree> list = sjqDeodorizationRunningThreeMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<SjqDeodorizationRunningThree> list = sjqDeodorizationRunningThreeMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return sjqDeodorizationRunningThreeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(SjqDeodorizationRunningThree record) {
        return sjqDeodorizationRunningThreeMapper.updateByPrimaryKeySelective(record);
    }
}
